       IDENTIFICATION DIVISION.
       AUTHOR. Mantese, Daniel    87958.
       AUTHOR. Ros, Gabriel Dario 88442.
       AUTHOR. Julian DAmbrosio 90252.
       AUTHOR. Federico Casares 89887.
       AUTHOR. Ezequiel De Luca 89779.
      *--------------------------------------------------------------
       PROGRAM-ID. PUNTOB.
      *--------------------------------------------------------------
       ENVIRONMENT DIVISION.
      *--------------------------------------------------------------
       CONFIGURATION SECTION.

       SPECIAL-NAMES.
           DECIMAL-POINT IS COMMA.

       INPUT-OUTPUT SECTION.

       FILE-CONTROL.
           SELECT AUTOS ASSIGN TO "AUTOS.DAT"
                                 ORGANIZATION IS LINE SEQUENTIAL
                                 ACCESS MODE IS SEQUENTIAL.
                                 
            SELECT ALQUILERES ASSIGN TO "ALQUILERES.DAT"
                                 ORGANIZATION IS LINE SEQUENTIAL
                                 ACCESS MODE IS SEQUENTIAL.
                                 
            SELECT ESTADISTICA ASSIGN TO "Estadisica(OUT).TXT"
                                 ORGANIZATION IS LINE SEQUENTIAL
                                 ACCESS MODE IS SEQUENTIAL.
      *--------------------------------------------------------------
       DATA DIVISION.
      *-------------------------------------------------------------
       FILE SECTION.
       FD  AUTOS        RECORDING MODE IS F
                        LABEL RECORD IS STANDARD.
       01  R-AUTOS.
           03 AUTOS-AUT-PATENTE                  PIC X(6).
           03 AUTOS-AUT-DESC                     PIC X(30).
           03 AUTOS-AUT-MARCA                    PIC X(20).
           03 AUTOS-AUT-COLOR                    PIC X(10).
           03 AUTOS-AUT-TAMANO                   PIC X.
           03 AUTOS-AUT-IMPORTE                  PIC 9(4)V99.		   

       FD  ALQUILERES       RECORDING MODE IS F
                            LABEL RECORD IS STANDARD.
       01  R-ALQUILERES.
           03 ALQ-PATENTE                        PIC X(6).
           03 ALQ-FECHA                          PIC 9(8).
           03 ALQ-TIPO-DOC                       PIC X.
           03 ALQ-NRO-DOC                        PIC X(20).
           03 ALQ-IMPORTE                        PIC 9(4)V99.
           
       FD  ESTADISTICA      RECORDING MODE IS F
                            LABEL RECORD IS OMITTED.
       01  R-EST                                 PIC X(80).
       
       WORKING-STORAGE SECTION.
      *****************************************************************
      *               DEFINICIONES     DE     TABLAS                  *
      *****************************************************************

       01  TABLA-MARCAPATENTE.
           03  T-MARCAPATENTE OCCURS 300 TIMES.
               05  MARCA                      PIC X(20).
               05  PATENTE                    PIC X(6).
        01  TABLA-ALQUILERES.
           03  T-ALQUILERES OCCURS 300 TIMES.
               05  ALQ-MARCA                      PIC X(20).
               05  T-MESES OCCURS 12 TIMES.
                   07  MES                    PIC 9(3) VALUE 0.
       
       01  TOTALES.
           03  TOTALES-MESES OCCURS 12 TIMES.
               05  TOTAL-MES                  PIC 9(3) VALUE 0.

      *****************************************************************
      *               VARIABLES   DE  IMPRESION                       *
      *****************************************************************                     
       01  E-FECHA-HOJA.
           03  FILLER                         PIC X(1) VALUE SPACES.
           03  FILLER                         PIC X(7) VALUE "FECHA ".
           03  E-DIA                          PIC 9(2).
           03  FILLER                         PIC X(1) VALUE "/".
           03  E-MES                          PIC 9(2).
           03  FILLER                         PIC X(3) VALUE "/20".
           03  E-ANIO                         PIC 9(2).
           03  FILLER                         PIC X(52) VALUE SPACES.
           03  FILLER                         PIC X(5) VALUE "HOJA ".
           03  E-NRO-HOJA                     PIC 9(3).
           
       01  E-TITULO.
           03  FILLER                         PIC X(15) VALUE SPACES.
           03  FILLER                         PIC X(50)
               VALUE "Listado de Estadistico de alquileres por mes".
       01  E-PRIMERA-FILA.
           03  FILLER                         PIC X(8) VALUE SPACES.
           03  FILLER                         PIC X(5)
               VALUE "MARCA".               
           03  FILLER                         PIC X(9) VALUE SPACES.
           
           03  FILLER                         PIC X(3) VALUE 'ENE'.
           03  FILLER                         PIC X(1) VALUE SPACES.
           03  FILLER                         PIC X(3) VALUE 'FEB'.
           03  FILLER                         PIC X(1) VALUE SPACES.
           03  FILLER                         PIC X(3) VALUE 'MAR'.
           03  FILLER                         PIC X(1) VALUE SPACES.
           03  FILLER                         PIC X(3) VALUE 'ABR'.
           03  FILLER                         PIC X(1) VALUE SPACES.
           03  FILLER                         PIC X(3) VALUE 'MAY'.
           03  FILLER                         PIC X(1) VALUE SPACES.           
           03  FILLER                         PIC X(3) VALUE 'JUN'.
           03  FILLER                         PIC X(1) VALUE SPACES.
           03  FILLER                         PIC X(3) VALUE 'JUL'.
           03  FILLER                         PIC X(1) VALUE SPACES.
           03  FILLER                         PIC X(3) VALUE 'AGO'.
           03  FILLER                         PIC X(1) VALUE SPACES.
           03  FILLER                         PIC X(3) VALUE 'SEP'.
           03  FILLER                         PIC X(1) VALUE SPACES.
           03  FILLER                         PIC X(3) VALUE 'OCT'.
           03  FILLER                         PIC X(1) VALUE SPACES.
           03  FILLER                         PIC X(3) VALUE 'NOV'.
           03  FILLER                         PIC X(1) VALUE SPACES.
           03  FILLER                         PIC X(3) VALUE 'DIC'.
           
           03  FILLER                         PIC X(4) VALUE SPACES.
           03  FILLER                         PIC X(5) VALUE 'Total'.
           
       01  E-LINEA.
           03  FILLER                         PIC X(40)
               VALUE "----------------------------------------".
           03  FILLER                         PIC X(40)
               VALUE "----------------------------------------".
       01  E-FILA.
           03  FILLER                         PIC X(1) VALUE SPACES.
           03  E-FILA-MARCA                   PIC X(20).
           03  E-FILA-MESES OCCURS 12 TIMES.
               05  FILLER                     PIC X(1) VALUE SPACES.
               05  E-FILA-MES                 PIC X(3).
           03  FILLER                         PIC X(4) VALUE SPACES.
           03  E-TOTAL                        PIC X(5).

       01  E-TOTALES.
           03  FILLER                         PIC X(7) VALUE SPACES.
           03  FILLER                         PIC X(7)
               VALUE "Totales".               
           03  FILLER                         PIC X(8) VALUE SPACES.
           
           03  E-TOTAL-MESES OCCURS 12 TIMES.
               05  E-TOTAL-MES                PIC 9(3).
               05  FILLER                     PIC X(1) VALUE SPACES.
           
           03  FILLER                         PIC X(3) VALUE SPACES.
           03  E-TOT-ABSOLUTO                 PIC 9(4) VALUE 0.
           
      *****************************************************************
      *               VARIABLES     AUXILIARES                        *
      *****************************************************************           
       01  FECHA-ACTUAL.
           03  SYS-ANIO                       PIC X(2).
           03  SYS-MES                        PIC X(2).
           03  SYS-DIA                        PIC X(2).
       01  FECHA-EXTRAER.
           03  EXT-ANIO                       PIC 9(4).           
           03  EXT-MES                        PIC 9(2).
           03  EXT-DIA                        PIC 9(2).                  
       01  CONT-MARCAPATENTE                  PIC 9(3).
       01  I-SORT                             PIC 9(3).
       01  J-SORT                             PIC 9(3).
       01  I-DEBUG                            PIC 9(3).
       01  J-DEBUG                            PIC 9(3).       
       01  I-IMPRESION                        PIC 9(3).
       01  I-MES                              PIC 9(3).
       01  I-RESTANTES                        PIC 9(3).
       01  I-PROC-ALQ                         PIC 9(3).
       01  J-PROC-ALQ                         PIC 9(3).
       01  CN-TOTAL-MESES                     PIC 9(4).
       01  CN-PAGINAS                         PIC 9(3) VALUE 0.
       01  CN-CANT-RESTANTE-EN-PAG            PIC 9(3) VALUE 0.
       01  CN-MATRIZ-ALQUILERES               PIC 9(3) VALUE 1.       
       01  CN-MATRIZ-MARCAPATENTE             PIC 9(3) VALUE 1.              
       01  TEMP-MARCA                         PIC X(20).
       01  TEMP-PATENTE                       PIC X(6).
       01  CLAVE-MARCA                        PIC X(20).
       01  CLAVE-PATENTE                      PIC X(6).

       01  WS-EOF.
           03  EOF-AUTOS                      PIC X(2) VALUE "NO".
               88  FIN-AUTOS                           VALUE "SI".
           03  EOF-ALQUILERES                 PIC X(2) VALUE "NO".
               88  FIN-ALQUILERES                      VALUE "SI".               

       01  CN-CONTADORES.
           03  CN-LEIDOS-AUTOS                PIC 9(3) VALUE 0.
           03  CN-AUTOS-REALES                PIC 9(3).
           03  CN-MARCAS-REALES               PIC 9(3) VALUE 0.
      *---------------------------------------------------------------
       PROCEDURE DIVISION.
       MAIN.
        DISPLAY "*** INICIANDO !!! ***" END-DISPLAY.
        PERFORM INICIO THRU F-INICIO.

        DISPLAY "*** CARGANDO MATRIZ MARCA"
         " PATENTE DE AUTOS !!! ***" END-DISPLAY.
        PERFORM CARGAR-MATRIZMARCAPATENTE
         THRU F-CARGAR-MATRIZMARCAPATENTE
          UNTIL FIN-AUTOS.

        DISPLAY "*** CARGANDO HIGH VALUE EN LO QUE"
         " RESTA DE MATRIZ MARCA PATENTE !!! ***" END-DISPLAY.            
        MOVE CN-LEIDOS-AUTOS TO I-RESTANTES.
        ADD 1 TO I-RESTANTES.
        PERFORM INI-MMP-EN-HIGH-VALUE
         THRU F-INI-MMP-EN-HIGH-VALUE VARYING
          I-RESTANTES FROM I-RESTANTES BY 1 UNTIL I-RESTANTES=300.

        DISPLAY "*** ORDENANDO MATRIZ MARCA PATENTE"
         " POR MARCA ***" END-DISPLAY.
        PERFORM ORDENAR-MMP-MARCA THRU F-ORDENAR-MMP-MARCA.

        DISPLAY "*** GENERANDO LA MATRIZ"
         " ALQUILERES Y CARGANDOLE LAS MARCAS!!! ***" END-DISPLAY.
        PERFORM GENERAR-MATRIZ-ALQUILERES 
         THRU F-GENERAR-MATRIZ-ALQUILERES.

        DISPLAY "*** ORDENANDO MATRIZ MARCA PATENTE"
         " POR PATENTE ***" END-DISPLAY.        
        PERFORM ORDENAR-MMP-PATENTE THRU F-ORDENAR-MMP-PATENTE.
        
        DISPLAY "*** PROCESANDO ALQUILERES !!! ***" END-DISPLAY.
        PERFORM PROCESAR-ALQUILERES THRU F-PROCESAR-ALQUILERES
         UNTIL FIN-ALQUILERES.
        
        DISPLAY "*** IMPRIMIENDO ESTADISTICAS DE"
         " ALQUILERES !!! ***" END-DISPLAY.
        PERFORM IMPRIMIR-MATRIZ-ALQUILERES 
         THRU F-IMPRIMIR-MATRIZ-ALQUILERES.

        DISPLAY "*** FINALIZANDO !!! ***" END-DISPLAY.      
        PERFORM FIN THRU F-FIN.
        DISPLAY "*** EXIT OK !!! ***" END-DISPLAY.           
        STOP RUN.

      *---------------------------------------------------------------
       INICIO.
        ACCEPT FECHA-ACTUAL FROM DATE.
        MOVE SYS-DIA TO E-DIA.
        MOVE SYS-MES TO E-MES.
        MOVE SYS-ANIO TO E-ANIO.
        OPEN INPUT AUTOS.
        OPEN INPUT ALQUILERES.
        OPEN OUTPUT ESTADISTICA.
       F-INICIO. EXIT.

      *---------------------------------------------------------------
       FIN.
        CLOSE AUTOS.
        CLOSE ALQUILERES.
        CLOSE ESTADISTICA.
       F-FIN. EXIT.

      *---------------------------------------------------------------
       CARGAR-MATRIZMARCAPATENTE.
        PERFORM LEER-AUTOS THRU F-LEER-AUTOS.
        IF NOT FIN-AUTOS THEN
            MOVE AUTOS-AUT-PATENTE TO PATENTE(CN-LEIDOS-AUTOS)
            MOVE AUTOS-AUT-MARCA TO MARCA(CN-LEIDOS-AUTOS)
        END-IF.
       F-CARGAR-MATRIZMARCAPATENTE. EXIT.

      *---------------------------------------------------------------
	  
       INI-MMP-EN-HIGH-VALUE.
        MOVE HIGH-VALUE TO PATENTE(I-RESTANTES).
        MOVE HIGH-VALUE TO MARCA(I-RESTANTES).
       F-INI-MMP-EN-HIGH-VALUE. EXIT.	   

      *---------------------------------------------------------------
       ORDENAR-MMP-MARCA.
        IF (CN-AUTOS-REALES > 1)
                PERFORM VARYING J-SORT FROM 2 BY 1
                 UNTIL J-SORT = CN-AUTOS-REALES + 1
                    MOVE MARCA(J-SORT) TO CLAVE-MARCA
                    MOVE PATENTE(J-SORT) TO CLAVE-PATENTE
                    COMPUTE I-SORT = J-SORT - 1
                    PERFORM UNTIL ((I-SORT < 0) 
                     OR (MARCA(I-SORT) <= CLAVE-MARCA))
                        MOVE MARCA(I-SORT) TO MARCA(I-SORT + 1)
                        MOVE PATENTE(I-SORT) TO PATENTE(I-SORT + 1)
                        SUBTRACT 1 FROM I-SORT
                    END-PERFORM
                    MOVE CLAVE-PATENTE TO PATENTE(I-SORT + 1)
                    MOVE CLAVE-MARCA TO MARCA(I-SORT + 1)
                END-PERFORM
        END-IF.
       F-ORDENAR-MMP-MARCA. EXIT.	   
       
      *---------------------------------------------------------------
       ORDENAR-MMP-PATENTE.
        IF (CN-AUTOS-REALES > 1)
                PERFORM VARYING J-SORT FROM 2 BY 1
                 UNTIL J-SORT = CN-AUTOS-REALES + 1
                    MOVE MARCA(J-SORT) TO CLAVE-MARCA
                    MOVE PATENTE(J-SORT) TO CLAVE-PATENTE
                    COMPUTE I-SORT = J-SORT - 1
                    PERFORM UNTIL ((I-SORT < 0) 
                     OR (PATENTE(I-SORT) <= CLAVE-PATENTE))
                        MOVE MARCA(I-SORT) TO MARCA(I-SORT + 1)
                        MOVE PATENTE(I-SORT) TO PATENTE(I-SORT + 1)
                        SUBTRACT 1 FROM I-SORT
                    END-PERFORM
                    MOVE CLAVE-PATENTE TO PATENTE(I-SORT + 1)
                    MOVE CLAVE-MARCA TO MARCA(I-SORT + 1)
                END-PERFORM
        END-IF.
       F-ORDENAR-MMP-PATENTE. EXIT.	   
       
      *---------------------------------------------------------------      
       GENERAR-MATRIZ-ALQUILERES.
       MOVE HIGH-VALUE TO TEMP-MARCA.
       PERFORM CARGAR-MARCA-A-ALQUILERES
         THRU F-CARGAR-MARCA-A-ALQUILERES VARYING
          CN-MATRIZ-MARCAPATENTE FROM 1 BY 1
           UNTIL CN-MATRIZ-MARCAPATENTE=300.
       PERFORM INI-MALQUILERES-EN-HIGH-VALUE
         THRU F-INI-MALQUILERES-EN-HIGH-VALUE VARYING
          I-RESTANTES FROM CN-MATRIZ-ALQUILERES
           BY 1 UNTIL I-RESTANTES=300.
       F-GENERAR-MATRIZ-ALQUILERES. EXIT.	   
       
      *---------------------------------------------------------------
       CARGAR-MARCA-A-ALQUILERES.
        IF ((TEMP-MARCA <> MARCA(CN-MATRIZ-MARCAPATENTE))
         AND (MARCA(CN-MATRIZ-MARCAPATENTE) <> HIGH-VALUE)) THEN
            MOVE MARCA(CN-MATRIZ-MARCAPATENTE) TO TEMP-MARCA
            MOVE MARCA(CN-MATRIZ-MARCAPATENTE) 
             TO ALQ-MARCA(CN-MATRIZ-ALQUILERES) 
            ADD 1 TO CN-MATRIZ-ALQUILERES
            ADD 1 TO CN-MARCAS-REALES
        END-IF.
       F-CARGAR-MARCA-A-ALQUILERES. EXIT.	   
       
      *--------------------------------------------------------------- 
       INI-MALQUILERES-EN-HIGH-VALUE.
        MOVE HIGH-VALUE TO ALQ-MARCA(I-RESTANTES).
       F-INI-MALQUILERES-EN-HIGH-VALUE. EXIT.	                    
      *---------------------------------------------------------------
       PROCESAR-ALQUILERES.
        PERFORM LEER-ALQUILERES THRU F-LEER-ALQUILERES.
        IF NOT FIN-ALQUILERES
            MOVE 1 TO I-PROC-ALQ
            MOVE 1 TO J-PROC-ALQ
            PERFORM UNTIL ((PATENTE(I-PROC-ALQ) = ALQ-PATENTE)
             OR (I-PROC-ALQ = 300))
                ADD 1 TO I-PROC-ALQ
            END-PERFORM
            IF PATENTE(I-PROC-ALQ) = ALQ-PATENTE
             THEN
                PERFORM UNTIL ((ALQ-MARCA(J-PROC-ALQ) 
                 = MARCA(I-PROC-ALQ)
                 OR (J-PROC-ALQ = 300)))
                        ADD 1 TO J-PROC-ALQ
                END-PERFORM
                IF ALQ-MARCA(J-PROC-ALQ) <> MARCA(I-PROC-ALQ)
                      DISPLAY "NO EXISTE LA MARCA !!!, LO
                       CUAL NUNCA DEBERIA OCCURRIR
                       DADO QUE EXISTE LA PATENTE" END-DISPLAY
                      STOP RUN
                END-IF
                MOVE ALQ-FECHA TO FECHA-EXTRAER
                ADD 1 TO MES (J-PROC-ALQ, EXT-MES)
                ADD 1 TO TOTAL-MES (EXT-MES)
            ELSE
                DISPLAY "NO EXISTE LA PATENTE !!!!"
                ALQ-PATENTE END-DISPLAY
            END-IF
        END-IF.
       F-PROCESAR-ALQUILERES. EXIT.	   

      *---------------------------------------------------------------
       IMPRIMIR-MATRIZ-ALQUILERES.
        IF (CN-CANT-RESTANTE-EN-PAG = 0)
            PERFORM IMPRIMIR-ENC-ALQUILERES
            THRU F-IMPRIMIR-ENC-ALQUILERES.
        PERFORM IMPRIMIR-MARCA THRU F-IMPRIMIR-MARCA VARYING
         I-IMPRESION FROM 1 BY 1 UNTIL I-IMPRESION > CN-MARCAS-REALES.
        PERFORM IMPRIMIR-TOTALES THRU F-IMPRIMIR-TOTALES.
       F-IMPRIMIR-MATRIZ-ALQUILERES. EXIT.	   
              
      *---------------------------------------------------------------
       IMPRIMIR-ENC-ALQUILERES.
        ADD 1 TO CN-PAGINAS.
        MOVE CN-PAGINAS TO E-NRO-HOJA.
        
        WRITE R-EST FROM E-FECHA-HOJA AFTER 1 LINE.
        WRITE R-EST FROM E-TITULO AFTER 1 LINE.
        WRITE R-EST FROM E-PRIMERA-FILA AFTER 2 LINE.
        WRITE R-EST FROM E-LINEA AFTER 1 LINE.
        WRITE R-EST FROM " ".
        SET CN-CANT-RESTANTE-EN-PAG TO 54.
        
       F-IMPRIMIR-ENC-ALQUILERES. EXIT.	   
              
      *---------------------------------------------------------------
       IMPRIMIR-MARCA.
        MOVE ALQ-MARCA(I-IMPRESION) TO E-FILA-MARCA.
        MOVE 0 TO CN-TOTAL-MESES.
        PERFORM VARYING I-MES FROM 1 BY 1 UNTIL I-MES > 12
            MOVE MES(I-IMPRESION, I-MES) TO E-FILA-MES(I-MES)
            ADD MES(I-IMPRESION, I-MES) TO CN-TOTAL-MESES
        END-PERFORM.
        MOVE CN-TOTAL-MESES TO E-TOTAL.
        WRITE R-EST FROM E-FILA.
        SUBTRACT 1 FROM CN-CANT-RESTANTE-EN-PAG.
       F-IMPRIMIR-MARCA. EXIT.

      *---------------------------------------------------------------
       IMPRIMIR-TOTALES.
        WRITE R-EST FROM E-LINEA.
        PERFORM VARYING I-MES FROM 1 BY 1 UNTIL I-MES > 12
            MOVE TOTAL-MES(I-MES) TO E-TOTAL-MES(I-MES)
            ADD TOTAL-MES(I-MES) TO E-TOT-ABSOLUTO
        END-PERFORM.
        WRITE R-EST FROM E-TOTALES.
       F-IMPRIMIR-TOTALES. EXIT.	   	   
              
      *---------------------------------------------------------------         
       LEER-AUTOS.
        READ AUTOS
               AT END
                   SET FIN-AUTOS TO TRUE
                   MOVE HIGH-VALUE TO AUTOS-AUT-MARCA
                   MOVE HIGH-VALUE TO AUTOS-AUT-PATENTE
                   MOVE CN-LEIDOS-AUTOS TO CN-AUTOS-REALES
               NOT AT END
                   ADD 1 TO CN-LEIDOS-AUTOS.
       F-LEER-AUTOS. EXIT.

      *---------------------------------------------------------------
       LEER-ALQUILERES.
        READ ALQUILERES
               AT END
                   SET FIN-ALQUILERES TO TRUE
                   MOVE HIGH-VALUE TO ALQ-PATENTE.
       F-LEER-ALQUILERES. EXIT.

